package com.nianfei.rabbitmq_delayqueue;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author cityre
 * @date 2019/1/8
 */
@Component
@Slf4j
public class RabbitProducer {
	private final RabbitTemplate rabbitTemplate;
	
	@Autowired
	public RabbitProducer(RabbitTemplate rabbitTemplate) {
		this.rabbitTemplate = rabbitTemplate;
	}
	
	public void sendDelayMessage(List<Integer> list) {
		//这里的消息可以是任意对象，无需额外配置，直接传即可 log.info("===============延时队列生产消息====================");
		log.info("发送时间:{},发送内容:{}", LocalDateTime.now(), list.toString());
		this.rabbitTemplate.convertAndSend("delay_exchange", "delay_key", list, message -> {
			//注意这里时间要是字符串形式
			message.getMessageProperties().setExpiration("6000");
			return message;
		});
		log.info("{}ms后执行", 6000);
		
	}
}
